package quickup;

import java.awt.Dimension;
import java.io.File;

import javax.swing.JLabel;
import javax.swing.JProgressBar;

public class FileUpload implements ProgressListener {

	private File file;
	private JProgressBar progressBar;
	private JLabel status;
	private boolean enabled;
	
	public FileUpload(File f) {
		file = f;
		progressBar = new JProgressBar();
		progressBar.setPreferredSize(new Dimension(100,progressBar.getHeight()));
		progressBar.setMinimum(0);
		progressBar.setMaximum((int)file.length());
		enabled = true;
		status = new JLabel("ready");
		JLabel filename = new JLabel(f.getName());
		filename.setPreferredSize(new Dimension(100,filename.getHeight()));
	}
	
	public void setEnabled(boolean b) {enabled = b;}
	public boolean isEnabled() {return enabled;}
	public File getFile() {
		return file;
	}
	
	public String getFileName() {
		return file.getAbsolutePath();
	}
	
	public int getFileSize() {
		return (int) Math.floor( (double) file.length() / 1024 );
	}
	
	public int getFileSizeBytes() {
		return (int)file.length();
	}

	public JProgressBar getProgressBar() {
		return progressBar;
	}

	public String getStatus() {
		return status.getText();
	}

	public void onStatusChange(Object origin, String text) {
		status.setText(text.replaceAll("\n"," "));
	}

	public void onProgressChange(Object origin, int position, int total) {
		//progressBar.setMaximum(total);
		progressBar.setValue(position);
	}

	public void onOperationFinished(Object origin) {
		progressBar.setValue(progressBar.getMaximum());
	}
}
